package com.oversea.action.course;

import java.util.Date;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.Course;
import com.oversea.models.Major;
import com.oversea.service.CourseService;
import com.oversea.util.BeanUtils;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;
import static com.oversea.util.ParamUtil.*;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/course")
@Permission(level = 3)
public class CourseAction extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;

	private CourseService courseService;
	
	private Course course;

	private List<Course> courseList;

	private Page page;

	private int objId;

	private List<Major> majorList;  //开设该课程的专业
	private String chineseName; // 学制
	private String englishName; // 学费范围

	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return Course.class;
	}

	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(chineseName)) {
			searchContext.addOption(new SearchOption("chineseName", "%"+chineseName+"%",
					SearchOption.Option.like));
		}
		if (isNotEmpty(englishName)) {
			searchContext.addOption(new SearchOption("englishName",
					"%"+englishName+"%", SearchOption.Option.like));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "toadd", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/course/course_add.ftl") })
	public String add() {
		return SUCCESS;
	}

	@Action(value = "toedit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/course/course_edit.ftl") })
	public String edit() {
		course = courseService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "toview", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/course/course_view.ftl") })
	public String view() {
		course = courseService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String save() {
		course.setCreatedTime(new Date());
		course.setModifiedTime(new Date());
		courseService.save(course);
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String update() {
		Course temp = courseService.getObjById(course.getId());
		course.setCreatedTime(temp.getCreatedTime());
		course.setModifiedTime(new Date());
		course.setMajorList(temp.getMajorList());
		BeanUtils.copyProperties(temp, course);
		courseService.update(temp);
		course = temp;
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/course/course_list.ftl") })
	public String list() {
		page = PageUtil.createPage(pageSize, currentPage, courseService
				.getObjectCount(getListSearchContext()).intValue());
		courseList = courseService.getObjectList(getListSearchContext(), pageSize,
				currentPage);
		return SUCCESS;
	}

	@Action(value = "del", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String del() {
		Course temp = courseService.getObjById(objId);
		courseService.delete(temp);
		return SUCCESS;
	}
	
	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public List<Major> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<Major> majorList) {
		this.majorList = majorList;
	}

	public String getChineseName() {
		return chineseName;
	}

	public void setChineseName(String chineseName) {
		this.chineseName = chineseName;
	}

	public String getEnglishName() {
		return englishName;
	}

	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}


}
